var timer = null,
  alpha = 30,
  speed = 0;
function opacityAnimation(obj, endAlpha) {
  console.log(obj, endAlpha);
  clearInterval(timer);
  timer = setInterval(function () {
    speed = endAlpha > alpha ? 5 : -5;
    if (alpha === endAlpha) {
      clearInterval(timer);
      return;
    }
    alpha += speed;
    obj.style.opacity = alpha / 100;
    obj.style.filter = `alpha(opacity:${alpha})`;
  }, 30);
}

//yunsuyundong
function startAnimation(obj, end) {
  speed = end > obj.offsetLeft ? 5 : -5;
  clearInterval(timer);
  console.log(obj.offsetLeft, end);
  timer = setInterval(function () {
    if (obj.offsetLeft === end) {
      clearInterval(timer);
      return;
    }
    console.log(obj.offsetLeft, speed);
    obj.style.left = obj.offsetLeft + speed + "px";
  }, 30);
}

// 缓动动画
function huangdongyundong(obj, end, fn) {
  clearInterval(timer);
  timer = setInterval(function () {
    speed = (end - obj.offsetLeft) / 20;
    speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
    if (obj.offsetLeft === end) {
      clearInterval(timer);
      if (fn) fn()
      return;
    }
    obj.style.left = obj.offsetLeft + speed + "px";
  }, 30);
}

// 多物体运动
function duowutiyundong(obj, attr, endTarget, fn) {
  //  console.log(obj)
  clearInterval(obj.timer)
  obj.timer = setInterval(() => {
    var offsetWidth = parseInt(getStyle(obj, attr))
    speed = (endTarget - offsetWidth) / 20
    speed = endTarget > offsetWidth ? Math.ceil(speed) : Math.floor(speed)
    if (endTarget === offsetWidth) {
      clearInterval(obj.timer)
      if (fn) fn()
      return
    }
    // console.log("=====",obj.offsetWidth,speed)
    obj.style[attr] = offsetWidth + speed + 'px'
  }, 30)
}
// 多物体大小运动和透明度兼容
function yundongAndToumin(obj, json, fn) {
  // json格式：{"opacity":30,'width':300}
  console.log("yundongAndToumin",json)
  clearInterval(obj.timer)
  obj.timer = setInterval(() => {
    var offsetCur = 0
    var flag =true //如果为true，所有属性达到终点值
    for (var attr in json) {
      switch (attr){
        case 'opacity':
            offsetCur = Math.round(parseFloat(getStyle(obj, attr)) * 100)
          break
        case 'scrollTop':
            offsetCur=obj[attr]
          break
        default:
            offsetCur = parseInt(getStyle(obj, attr))
          break;
     }
      speed = (json[attr] - offsetCur) / 20
      speed = json[attr] > offsetCur ? Math.ceil(speed) : Math.floor(speed)
      
      if (json[attr] !== offsetCur) {
        flag=false
      }
      switch (attr){
        case 'opacity':
          obj.style[attr]= `alpha(opacity:${offsetCur+speed})`;
          obj.style[attr]= (offsetCur+speed)/ 100;
          break
        case 'scrollTop':
          obj.scrollTop=offsetCur+speed
          break
        default:
          obj.style[attr] = offsetCur + speed + 'px'
          break;
     }

    }
    if (flag) {
      clearInterval(obj.timer)
      if (fn) fn()
      return
    }
  }, 30)
}

//获取物体的样式
function getStyle(obj, attr) {
  if (obj.currentStyle) {
    return obj.currentStyle[attr]
  } else {
    return getComputedStyle(obj, null)[attr]
  }
}

export default {
  opacityAnimation,
  startAnimation,
  huangdongyundong,
  duowutiyundong_wrong,
  duowutiyundong,
  getStyle,
  yundongAndToumin,
};


// 多物体运动有问题
function duowutiyundong_wrong(obj, endTarget) {
  //  console.log(obj)
  clearInterval(obj.timer)
  obj.timer = setInterval(() => {
    speed = (endTarget - obj.offsetWidth) / 20
    speed = endTarget > obj.offsetWidth ? Math.ceil(speed) : Math.floor(speed)
    if (endTarget === obj.offsetWidth) {
      clearInterval(obj.timer)
      return
    }
    // console.log("=====",obj.offsetWidth,speed)
    obj.style.width = obj.offsetWidth + speed + 'px'
  }, 30)
}
